package com.mayisoftware.mayioa.common.impl.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 *
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String HH_MM = "HH:mm";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date covertToDate(final String format, final String ts)
    {
        try
        {
            return HSSFDateUtil.getJavaDate(Double.parseDouble(ts));
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    public static String getDiffByParaMinutes(List<Date> dates, int minutes){

        String ret = "";
        boolean isAdded = false;
        if (dates==null ||dates.size()==0){
            return "";
        }
        Date srcDate = dates.get(0);
        long maxDistance = 0;
        for (int i=1;i<dates.size();++i) {
            Date tgtDate= dates.get(i);
            long dis = diff(srcDate,tgtDate);
            if (dis>maxDistance&&dis>minutes*60*1000){
                maxDistance = dis;
                ret = parseDateToStr("HH:mm",tgtDate);
            }
        }

        return ret;
    }

    public static double diff(String srcDate, String tgtDate){

        if (srcDate==null || tgtDate==null){
            return 0;
        }

        try {
            Date src = dateTime(YYYY_MM_DD_HH_MM, srcDate);
            Date tgt = dateTime(YYYY_MM_DD_HH_MM, tgtDate);
            long millSeconds = diff(src,tgt);
            if (Integer.MAX_VALUE==millSeconds){
                return 0;
            }
            return  millSeconds/1000/3600.0;
        }catch (Exception ex){
            return 0;
        }
    }

    public  static  long diff(Date srcDate,Date tgtDate){

        if (srcDate==null || tgtDate==null){
            return  Integer.MAX_VALUE;
        }
        return Math.abs(srcDate.getTime()-tgtDate.getTime());
    }

}
